<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.PurreqDAO"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	PurreqDAO purreqDao = (PurreqDAO)ctx.getBean("PurreqDAO");
	
	Integer teamId = (Integer)session.getAttribute("tid");
	
	String pnStr = request.getParameter("pn");
	String codeType = request.getParameter("codeType");
	String code = request.getParameter("code");
	String dateType = request.getParameter("dateType");
	String date = request.getParameter("date");
	
	int pn = 0;
	int pageSize = 10;
	
	if(pnStr != null && !"".equals(pnStr.trim())){
		try{
			pn = Integer.parseInt(pnStr);
		}catch(Exception e){
			pn = 0;
		}
	}
	
	List where = new ArrayList();
	
	where.add(new Object[]{"eq", "state", 4});
	where.add(new Object[]{"eq", "teamId", teamId});
	
	if(code != null && !"".equals(code)){
		if("1".equals(codeType))
			where.add(new Object[]{"like", "reqCode", code});
		else if("2".equals(codeType))
			where.add(new Object[]{"like", "p.code", code});
	}
	
	if(date != null && !"".equals(date)){
		Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(date);
		
		if("1".equals(dateType))
			where.add(new Object[]{"eq", "reqDate", dateVal});
		else if("2".equals(dateType))
			where.add(new Object[]{"eq", "buyDate", dateVal});
	}
	
	Map map = purreqDao.getPurreq(pn,pageSize,
	new String[][]{new String[]{"desc","date"}},
	where,
	new String[][]{new String[]{"","id"},new String[]{"","reqCode"},new String[]{"","p.code"},new String[]{"","p.name"},new String[]{"","reqDate"},new String[]{"","buyDate"}}
	,new String[]{"project"});

	request.setAttribute("map",map);
	
	int pageTotal = ((Integer)map.get("pageTotal")).intValue();
	int pageNo = ((Integer)map.get("pageNo")).intValue();
	List pageList=getPage(pageTotal,pageNo);
	request.setAttribute("pages",pageList);
	
	request.setAttribute("codeType",codeType);
	request.setAttribute("code",code);
	request.setAttribute("dateType",dateType);
	request.setAttribute("date",date);
	
	pageContext.forward("/vm/procure/procureOrder.vm");
%>